package com.cyzy.service.impl;

import com.cyzy.mapper.MiniprogramHomeMapper;
import com.cyzy.pojo.*;
import com.cyzy.service.MiniprogramHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 小程序首页数据Service实现类
 * @author yangJingShi
 * @date 2025/1/19
 */
@Service
public class MiniprogramHomeServiceImpl implements MiniprogramHomeService {

    @Autowired
    private MiniprogramHomeMapper miniprogramHomeMapper;

    /**
     * 服务器基础URL
     */
    private static final String SERVER_BASE_URL = "http://localhost:5217";

    /**
     * 处理图片URL，将相对路径转换为完整URL
     */
    private String processImageUrl(String imageUrl) {
        if (imageUrl == null || imageUrl.trim().isEmpty()) {
            return imageUrl;
        }
        // 如果已经是完整URL，直接返回
        if (imageUrl.startsWith("http://") || imageUrl.startsWith("https://")) {
            return imageUrl;
        }
        // 如果是相对路径，添加服务器基础URL
        if (imageUrl.startsWith("/")) {
            return SERVER_BASE_URL + imageUrl;
        }
        return SERVER_BASE_URL + "/" + imageUrl;
    }

    @Override
    public List<SuBannerAds> getBannerList() {
        List<SuBannerAds> banners = miniprogramHomeMapper.selectActiveBanners();
        return banners.stream().map(banner -> {
            banner.setImage(processImageUrl(banner.getImage()));
            return banner;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SuExternalGoods> getGoodsList() {
        List<SuExternalGoods> goods = miniprogramHomeMapper.selectActiveGoods();
        return goods.stream().map(good -> {
            good.setImage(processImageUrl(good.getImage()));
            return good;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SuQuickEntries> getQuickEntries() {
        return miniprogramHomeMapper.selectActiveQuickEntries();
    }

    @Override
    public List<SuFunctionEntries> getFunctionEntries() {
        List<SuFunctionEntries> entries = miniprogramHomeMapper.selectActiveFunctionEntries();
        return entries.stream().map(entry -> {
            entry.setCustomIcon(processImageUrl(entry.getCustomIcon()));
            return entry;
        }).collect(Collectors.toList());
    }

    @Override
    public List<SuCampusAds> getNoticeList() {
        return miniprogramHomeMapper.selectActiveNotices();
    }

    @Override
    public List<SuBrandRecommendations> getRecommendList() {
        List<SuBrandRecommendations> recommendations = miniprogramHomeMapper.selectActiveRecommendations();
        return recommendations.stream().map(recommendation -> {
            recommendation.setLogo(processImageUrl(recommendation.getLogo()));
            return recommendation;
        }).collect(Collectors.toList());
    }
} 